﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using TSB.FarmPowerShellWizard.Repository;

namespace TSB.FarmPowerShellWizard.Layouts.TSB.FarmPowerShellWizard
{
    public partial class PSW_ServicesOnServer : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var serverList = new FarmRepository().GetServers();

                if (serverList.Count > 1)
                {
                    BindServerList(serverList);
                }
                else
                {
                    GenerateScript(serverList[0]);
                }
            }
        }

        void BindServerList(List<string> servers)
        {
            foreach (var server in servers)
            {
                ddServerList.Items.Add(server);
            }
        }

        void GenerateScript(string serverName)
        {
            var script = new ServicesRepository().GenerateScript(serverName);

            SetPowerShellText(script);
        }

        void SetPowerShellText(string script)
        {
            if (string.IsNullOrEmpty(script))
            {
                phScriptContainer.Visible = false;
                txtPowerShell.Text = string.Empty;
                return;
            }

            phScriptContainer.Visible = true;
            txtPowerShell.Text = script;
        }
    }
}
